<template>
	<view class="page-conent">
		<view style="height: var(--status-bar-height);"></view>
		<popup @close="hide = false" :hide="hide">
			<seachBar :teachersNumbers="resultObj.teachersNumbers" @change="initteacher" />
		</popup>
		<pagetitle :text="'填写检查'" />
		<view class="app-card" style="margin-top: 0px;">
			检查地址:<br/>
			{{itemInfo.address}}
		</view>
		<view class="app-card">
			责任教师: {{itemInfo.teacher}}
		</view>
		<view @click="hide = true" class="app-card" style="position: relative;">
			<view>其他相关教师</view>
			<view :key="index" v-for="(item,index) in teacher" class="teacher">{{item}}</view>
			<view class="right"></view>
		</view>
		<!-- <component v-if="sync" :resObj.sync="resultObj" @change="result" :is="istype"></component> -->
		<tabA :resObj.sync="resultObj" @change="result" v-if="type[0] && sync" />
		<tabB :resObj.sync="resultObj" @change="result" v-if="type[1] && sync"/>
		<tabC :resObj.sync="resultObj" @change="result" v-if="type[2] && sync"/>
		<tabD :resObj.sync="resultObj" @change="result" v-if="type[3] && sync"/>
		<view class="app-card">
			<view>上传图片</view>
			<imgUp :image="image" @unload="load" />
		</view>
		<view class="app-card remik">
			<view class="remik">是否通知负责人</view>
			<switch :checked="remik" @change="remik = !remik"></switch>
		</view>
		<view class="app-card" v-if="remik">
			<textarea value="remikVal" auto-height=true placeholder="整改要求" class="remik-inp" type="text" />
		</view>
		<view @click="submit" class="btn-submit">{{itemInfo.isWrited ? '修改检查':'提交检查'}}</view>
	</view>
</template>

<script>
	import { putcheck,getcheckInfoByid,putcheckInfoByid,httpCheck } from '../../request/check.js'
	
	import pagetitle from '../../components/pageHeader.vue'
	import seachBar from '../../components/seachBar.vue'
	import popup from '../../components/popup.vue'
	import imgUp from '../../components/imgUpload.vue'
	
	import tabA from './components/7s.vue'
	import tabC from './components/study.vue'
	import tabB from './components/teacher.vue'
	import tabD from './components/dorm.vue'
	export default {
		onLoad: function (option) { 
			this.checkType = option.checkType;
			this.type[option.checkType] = true
			this.istype = this.type[option.checkType];
		},
		data() {
			return {
				sync: false,
				checkType:0,//检查类型
				resultObj:{}, //全部检查结果
				teacher:[],  //反馈其他老师
				resInfo: {}, //各个检查的信息
				imgAddress: '',
				hide:false,		
				remik: false,  //是否反馈
				remikVal: '',
				// type:['tabA','tabB','tabC','tabD'],
				type:[false,false,false,false],
				istype:'',
				image:'',
				itemInfo: {},
			};
		},
		async mounted() {
			const item = uni.getStorageSync('checkinfo')
			if(item.inspectId){
				const resobj = await getcheckInfoByid(this.checkType,item.inspectId)
				this.resultObj = resobj;
				console.log(resobj)
				let teachers = [];
				if(resobj.teachersNumbers.length > 0){
					teachers = resobj.teachersNumbers.split('/').map( ele => ele.split("@")[0])
				}
				this.image = resobj.image;
				this.teacher = teachers
				this.remik = resobj.feedback;
				this.remikVal = resobj.requires;
			}
			this.itemInfo.address = item.roomName || item.buildings + item.room
			this.itemInfo.id = item.id
			this.itemInfo.teacher = item.responsibleName || item.classTeacheres
			this.itemInfo.isWrited = item.isWrited
			this.itemInfo.inspectId = item.inspectId
			this.sync = true
		},
		methods:{
			load(data){
				this.imgAddress = data.data.address
			},
			initteacher(target){
				const ter = [];
				const terNum = [];
				target.forEach( ele => {
					ter.push(ele.teacherName)
					terNum.push(ele.teacherNumber)
				})
				this.teacher = this.teacher.concat(ter)
				this.resultObj.teacherNumbers = terNum
			},
			result(res){
				this.resInfo = res;
			},
			async submit(){
				uni.showLoading()
				const checkinfo = {
					id: +this.itemInfo.id,
					feedback:this.remik,
					requires:this.remikVal,
					image:this.imgAddress,
					...this.resultObj,
					...this.resInfo
				}
				checkinfo.image = this.imgAddress
				console.log(checkinfo)
				let res;
				if(this.itemInfo.inspectId == null){
					res =  await httpCheck(this.checkType,"write",checkinfo,"POST")
				}else{
					checkinfo.inspectId = this.itemInfo.inspectId
					checkinfo.imageAddress = this.imgAddress
					res =  await putcheckInfoByid(this.checkType,checkinfo)
				}
				uni.hideLoading()
				console.log(JSON.stringify(res))
				if(res == "添加成功" || res == "修改成功"){
					uni.redirectTo({
						url:`/pages/check/check?checkType=${this.checkType}`,
					})
				}else{
					uni.showToast({
						url:"上传错误"
					})
				}
			}
		},
		components:{
			tabA,
			tabB,
			tabC,
			tabD,
			pagetitle,
			seachBar,
			popup,
			imgUp,
		}
	}
</script>

<style lang="scss" scoped>
.page-conent{
	padding: 0px 30px;
	font-size:14px;
	font-family:PingFang SC;
	font-weight:bold;
	color:rgba(57,66,91,1);
	.teacher{
		display: inline-block;
		font-size:11px;
		font-family:PingFang SC;
		font-weight:bold;
		color:rgba(57,66,91,1);
		padding: 0 5px;
	}
	.right{
		position: absolute;
		right: 30px;
		top: 50%;
		transform: translateY(-50%);
		width: 12px;
		height: 12px;
		background-image: url('../../static/img/icon_fill_right@3x.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
	}
	.btn-submit{
		background:rgba(59,96,255,1);
		border-radius:22px;
		font-size:18px;
		font-family:PingFang SC;
		font-weight:bold;
		color:rgba(255,255,255,1);
		text-align: center;
		height: 44px;
		line-height: 44px;
	}
	.remik{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
}
</style>
